package com.yuanduo_app.utils.update;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.xujl.fastlib.http.ApiDisposableObserver;
import com.xujl.fastlib.http.BaseResponse;
import com.xujl.fastlib.http.NetworkManager;
import com.xujl.fastlib.utils.NumberUtils;
import com.xujl.fastlib.utils.ResUtils;
import com.xujl.fastlib.utils.StringUtils;

import com.yuanduo_app.consts.BundleKey;
import com.yuanduo_app.entity.UpdateInfo;
import com.yuanduo_app.http.DataSource;
import com.yuanduo_app.http.RequestCommonParams;
import com.yuanduo_app.service.UpdateService;

/**
 * 版本更新帮助类
 */
public class VersionHelper {
    public static final String TAG = "VersionHelper";
    private VersionCheckCallback mVersionCheckCallback;
    private Context mContext;
    private UpdateInfo mUpdateInfo;
    private UpdateInfoWindow infoWindow;

    private VersionHelper (Context context) {
        mContext = context;

    }


    public static VersionHelper newInstance (Context context) {
        return new VersionHelper(context);
    }

    public void checkVersion (final VersionCheckCallback versionCheckCallback) {
        this.checkVersion(versionCheckCallback, true);
    }

    public void checkVersion (final VersionCheckCallback versionCheckCallback, boolean isLoading) {
        setVersionCheckCallback(versionCheckCallback);
        final RequestCommonParams.Builder builder = RequestCommonParams.createParamBuilder();
        builder.addParams("appName", "缘多新联盟")
                .addParams("type", "android");
        NetworkManager.execute(DataSource.createApi().queryUpdateInfo(builder.build()), new ApiDisposableObserver<UpdateInfo>(null) {
            @Override
            public void onResult (BaseResponse<UpdateInfo> response, UpdateInfo data) {
                if (data == null || ResUtils.getAppVersionCode() >= NumberUtils.longOf(data.getVersionNum())) {
                    versionCheckCallback.versionIsNew();
                    return;
                }
                if (StringUtils.isEmpty(data.getAppFileId())) {
                    versionCheckCallback.versionIsNew();
                    return;
                }
                mUpdateInfo = data;
                if (infoWindow == null) {
                    infoWindow = new UpdateInfoWindow(mContext, VersionHelper.this);
                }
                infoWindow.setData(data);
                infoWindow.showPopupWindow();
            }

            @Override
            public void onResultFailed (String code, String msg) {
                super.onResultFailed(code, msg);
                versionCheckCallback.onVersionFail();
            }

        }.setEnableLoadingDialog(isLoading));
    }

    public void setVersionCheckCallback (VersionCheckCallback versionCheckCallback) {
        mVersionCheckCallback = versionCheckCallback;
    }

    public VersionCheckCallback getVersionCheckCallback () {
        return mVersionCheckCallback;
    }

    public void startDownloadService () {
        Intent intent = new Intent(mContext, UpdateService.class);
        final Bundle bundle = new Bundle();
        bundle.putString(BundleKey.URL,  mUpdateInfo.getAppFileId());
        bundle.putString(BundleKey.VERSION, mUpdateInfo.getVersionName());
        bundle.putInt(BundleKey.COMMAND, UpdateService.COMMAND_DOWNLOAD);
        intent.putExtras(bundle);
        mContext.startService(intent);
    }

    public interface VersionCheckCallback {
        /**
         * 版本校验通过
         */
        void onVersionPass ();

        /**
         * 当前已是最新版本
         */
        void versionIsNew ();

        /**
         * onVersionPass
         * 校验未通过（需要强制更新时回调）
         */
        void onVersionRefuse ();

        /**
         * 版本校验失败
         */
        void onVersionFail ();
    }


}
